<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Go 包管理之module</title>
  
    <meta name="author" content="jouyouyun">

    <!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->

    <!-- Le styles -->
    <link href="/assets/twitter/stylesheets/bootstrap.min.css" type="text/css" rel="stylesheet" media="all">
<link href="/assets/twitter/stylesheets/style.css" type="text/css" rel="stylesheet" media="all">
<link href="/assets/twitter/widgets/google_prettify/stylesheets/twitter-bootstrap.css" type="text/css" rel="stylesheet" media="all">
 

    <!-- Le fav and touch icons -->
  <!-- Update these with your own images
    <link rel="shortcut icon" href="images/favicon.ico">
    <link rel="apple-touch-icon" href="images/apple-touch-icon.png">
    <link rel="apple-touch-icon" sizes="72x72" href="images/apple-touch-icon-72x72.png">
    <link rel="apple-touch-icon" sizes="114x114" href="images/apple-touch-icon-114x114.png">
  -->
  </head>

  <body>

    <div class="navbar">
      <div class="navbar-inner">
        <div class="container">
          <a class="brand" href="/">Jouyouyun&#39;s Blog</a>
          <ul class="nav">
            
              


  <li><a href="/archive">Archive</a></li>


            
              


  <li><a href="/tags">Tags</a></li>


            
              


  <li><a href="/categories">Categories</a></li>


            
              


  <li><a href="/about">About Me</a></li>


            
          </ul>
        </div>
      </div>
    </div>

    <div class="container">

      <div class="content">
        <div class="page-header">
  <h1>Go 包管理之module </h1>
</div>

<div class="row">
  <div class="span8">
    <h1>Go Module 使用</h1>

<p><code>Go Module</code> 是官方用来管理 <code>package</code> 依赖的工具, <code>1.11</code> 新加的, 使用时需要设置变量: <code>GO111MODULE=on</code> .</p>

<p><code>go mod help</code> 可以查看其帮助文档:</p>

<pre><code class="language-shell">$ go help mod
Go mod provides access to operations on modules.

Note that support for modules is built into all the go commands,
not just 'go mod'. For example, day-to-day adding, removing, upgrading,
and downgrading of dependencies should be done using 'go get'.
See 'go help modules' for an overview of module functionality.

Usage:

        go mod &lt;command&gt; [arguments]

The commands are:

        download    download modules to local cache
        edit        edit go.mod from tools or scripts
        graph       print module requirement graph
        init        initialize new module in current directory
        tidy        add missing and remove unused modules
        vendor      make vendored copy of dependencies
        verify      verify dependencies have expected content
        why         explain why packages or modules are needed

Use &quot;go help mod &lt;command&gt;&quot; for more information about a command.
</code></pre>

<p>下面来说明下如何使用.</p>

<hr />

<h2>示例</h2>

<p><code>go mod</code> 要求项目必须在 <code>GOPATH</code> 中, 所以在这个例子中设置为当前目录(<code>~/GoLang</code>) <code>export GOPATH=$PWD</code>, 然后在 <code>src</code> 下创建项目 <code>test</code> , 目录结构如下:</p>

<pre><code class="language-shell">GoLang
└── src
    └── test
        └── main.go
</code></pre>

<p>示例的代码请见: <a href="https://github.com/jouyouyun/examples/tree/master/GoModule">GoModule</a></p>

<h3>初始化</h3>

<p>进入 <code>test</code> 所在目录, 执行 <code>go mod init</code> 即可完成初始化, 会多出一个文件: <code>go.mod</code> .</p>

<h3>记录依赖</h3>

<p>执行 <code>go get ./</code> 即会开始查找依赖并下载记录到 <code>go.mod</code> 文件中.</p>

<p><strong><code>go mod</code> 会将依赖下载到 <code>$GOPATH/pkg</code> 下, 用来当作缓存, 可以在多个项目之间共享.</strong></p>

<h3>依赖变更及 vendor</h3>

<p>如在项目的开发过程中, 依赖有变更, 可使用 <code>go mod tidy</code> 来应用这些变更到 <code>go.mod</code> 文件.</p>

<p>在项目发布时会要将依赖复制到项目中, 此时使用 <code>go mod vendor</code> 即可完成此操作.</p>

<hr />

<p>到此就介绍完了 <code>go mod</code> 的用法, 其他的子命令用法请查看帮助文档.</p>

<p>最后在说下需要注意的地方:</p>

<ol>
<li><strong>项目必须要在 <code>GOPATH</code> 中</strong></li>
<li><strong>需要设置 <code>GO111MODULE=on</code> 变量</strong></li>
</ol>

    <hr>
    <div class="pagination">
      <ul>
        <ul>
          
            <li class="prev"><a href="/Blog/python3-%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0/" title="python3 学习笔记">&larr; Previous</a></li>
          
          

            <li><a href="/archive">Archive</a></li>

          
            <li class="next"><a href="/Blog/Linux-Signal-Examples/" title="Linux Signal Examples">Next &rarr;</a></li>
          
          
        </ul>
      </ul>
    </div>
    <hr>
    
<div id="disqus_thread"></div>
<script>
    var disqus_developer = 1;
    var disqus_shortname = 'jekyllbootstrap'; // required: replace example with your forum shortname
    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
        var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
        dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">blog comments powered by <span class="logo-disqus">Disqus</span></a>

  </div>
  
  <div class="span4">
    <h4>Published</h4>
    <div class="date"><span>2018-11-27</span></div>
    <br>
    <h4>Categories</h4>
    <ul class="tag_box">
    
      <li>
  <a href="/categories/#Blog-ref">Blog <span>23</span></a>
</li>
    
    </ul>
    <br>
    <h4>Tags</h4>
    <ul class="tag_box">
    
      <li>
  <a href="/tags/#go-ref">go <span>6</span></a>
</li>
    
      <li>
  <a href="/tags/#module-ref">module <span>1</span></a>
</li>
    
      <li>
  <a href="/tags/#package-management-ref">package management <span>1</span></a>
</li>
    
      <li>
  <a href="/tags/#gom-ref">gom <span>1</span></a>
</li>
    
    </ul>
  </div>
</div>

      </div>

      <footer>
        <p>&copy; jouyouyun 2013 
          with help from <a href="http://github.com/wendal/gor" target="_blank" title="Gor -- Fast Blog">Gor</a>
          and <a href="http://twitter.github.com/bootstrap/" target="_blank">Twitter Bootstrap</a>
		  and Idea from <a href="http://ruhoh.com" target="_blank" title="The Definitive Technical Blogging Framework">ruhoh</a>
        </p>
      </footer>

    </div> <!-- /container -->

    
<script src="//cdnjscn.b0.upaiyun.com/libs/prettify/r298/prettify.min.js"></script>
<script>
  var pres = document.getElementsByTagName("pre");
  for (var i=0; i < pres.length; ++i) {
    pres[i].className = "prettyprint linenums";
  }
  prettyPrint();
</script>

    
<script type="text/javascript">

  var _gaq = _gaq || [];
  var pluginUrl = '//www.google-analytics.com/plugins/ga/inpage_linkid.js';
  _gaq.push(['_require', 'inpage_linkid', pluginUrl]);
  _gaq.push(['_setAccount', 'UA-123-12']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  })();

</script>
  </body>
</html>
